package com.huntero.hweather.widget;

import java.util.ArrayList;
import java.util.List;

import com.huntero.hweather.R;
import com.huntero.hweather.adapter.CityCodeAdapter;
import com.huntero.hweather.bean.CityCode;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class HSpinner extends Spinner implements android.widget.AdapterView.OnItemClickListener{

	private SpinnerDialog mDialog;
	private List<CityCode> mData=null;
	private CityCode selected=null;
	
	public HSpinner(Context context, AttributeSet attrs) {
		super(context, attrs);
		selected = this.mData==null||this.mData.isEmpty()?null:this.mData.get(0);
		
	}

	private void init(Context context) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.cus_spinner, this);
//		ViewUtils.inject(this,view);
		
		
	}
	
	@Override
	public boolean performClick() {
		Context context = getContext();
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.spinner_dialog, null);  
        final ListView listview = (ListView) view  
                .findViewById(R.id.spinner_listview);  
        CityCodeAdapter adapters = (CityCodeAdapter) getAdapter();
        this.mData = adapters.getListData();
        listview.setAdapter(adapters);  
        listview.setOnItemClickListener(this);  
        mDialog = new SpinnerDialog(context, R.style.SpinnerDialog);//创建Dialog并设置样式主题  
        LayoutParams params = new LayoutParams(650, LayoutParams.MATCH_PARENT);  
        mDialog.setCanceledOnTouchOutside(true);// 设置点击Dialog外部任意区域关闭Dialog  
        mDialog.show();  
        mDialog.addContentView(view, params);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		setSelection(position);
		setSelectedValue(this.mData.get(position));
		if(this.mDialog != null){
			this.mDialog.dismiss();
			this.mDialog=null;
		}
	}
	
	private void setSelectedValue(CityCode cityCode) {
		this.selected = cityCode;
	}
	public CityCode getSelectedValue(){
		return this.selected;
	}

	public List<CityCode> getData(){
		return this.mData;
	}
	public void setData(List<CityCode> data){
		this.mData = data;
	}
	public void setTitle(String title){
		
	}
	
}
